#File Name: rep_dem_barplots.R
#Data: Multiple
#Purpose: Draw figures A.26 and A.27 for Richardson, "Politicization and Expertise: Exit, Effort, and Investment" in the appendix
#Output: Figures A.26 and A.27 for the appendix
#Date: 7/27/2016

library(readstata13)

x<-read.dta13(file="z:/MDR/sfgs1_pid.dta",convert.factors = F)
y<-read.dta13(file="G:/data/mdr/sfgs2_pid.dta",convert.factors = F)

x$rep<-NA
x$rep[(x$pid5_sfgs1==4|x$pid5_sfgs1==3)&
        (x$appointee_sfgs1=="careerist"|x$appointee_sfgs1=="ca"|x$appointee_sfgs1=="sfs")]<-1

x$dem<-NA
x$dem[(x$pid5_sfgs1==0|x$pid5_sfgs1==1)&
        (x$appointee_sfgs1=="careerist"|x$appointee_sfgs1=="ca"|x$appointee_sfgs1=="sfs")]<-1

y$rep<-NA
y$rep[y$pid_5==4|y$pid_5==3&
        (y$appointee_sfgs2=="careerist"|y$appointee_sfgs2=="ca"|y$appointee_sfgs2=="sfs")]<-1

y$dem<-NA
y$dem[y$pid_5==0|y$pid_5==1&
        (y$appointee_sfgs2=="careerist"|y$appointee_sfgs2=="ca"|y$appointee_sfgs2=="sfs")]<-1

barplot(table(x$rep, x$pol_app1))
barplot(table(y$rep, y$pol_app2))

barplot(table(x$dem, x$pol_app1))
barplot(table(y$dem, y$pol_app2))

################################################
#draw plots with percentages ####
################################################

#reps
pdf(file="C:/Users/richar33/Dropbox/Papers/expertise/paper/Figures/reps_pol.pdf",width=14,height=7)
par(mfrow=c(1,2),cex.lab=1.5,cex.axis=1.5,las=1,mar=c(5,4.5,4,2)+0.1,cex.main=1.5)

t<-table(x$rep, x$pol_app1)
sum(as.numeric(as.character(t)))
tc<-round(100*as.numeric(as.character(t))/sum(as.numeric(as.character(t))),digits=2)
bp<-barplot(t,ylab="", xlab="Percieved Politicization (N=363)",main="2007-2008",ylim=c(0,220))
title(ylab="Frequency", line=3.5) #adjust distance of y-axis label from text.
sp<-7
text(bp,(t+sp),tc,xpd=NA,cex=1.3)

t<-table(y$rep, y$pol_app2)
tc<-round(100*as.numeric(as.character(t))/sum(as.numeric(as.character(t))),digits=2)
sum(as.numeric(as.character(t)))
bp<-barplot(t,ylab="", xlab="Percieved Politicization (N=584)",main="2014",ylim=c(0,220))
title(ylab="Frequency", line=3.5) #adjust distance of y-axis label from text.
sp<-7
text(bp,(t+sp),tc,xpd=NA,cex=1.3)

dev.off()

#dems
pdf(file="C:/Users/richar33/Dropbox/Papers/expertise/paper/Figures/dem_pol.pdf",width=14,height=7)
par(mfrow=c(1,2),cex.lab=1.5,cex.axis=1.5,las=1,mar=c(5,4.5,4,2)+0.1,cex.main=1.5)

t<-table(x$dem, x$pol_app1)
sum(as.numeric(as.character(t)))
tc<-round(100*as.numeric(as.character(t))/sum(as.numeric(as.character(t))),digits=2)
bp<-barplot(t,ylab="", xlab="Percieved Politicization (N=1,042)",main="2007-2008",ylim=c(0,700))
title(ylab="Frequency", line=3.5) #adjust distance of y-axis label from text.
sp<-20
text(bp,(t+sp),tc,xpd=NA,cex=1.3)

t<-table(y$dem, y$pol_app2)
tc<-round(100*as.numeric(as.character(t))/sum(as.numeric(as.character(t))),digits=2)
sum(as.numeric(as.character(t)))
bp<-barplot(t,ylab="", xlab="Percieved Politicization (N=1,756)",main="2014",ylim=c(0,700))
title(ylab="Frequency", line=3.5) #adjust distance of y-axis label from text.
sp<-2-
text(bp,(t+sp),tc,xpd=NA,cex=1.3)

dev.off()


#Test distributions for independence

y$rep[y$rep==1]<-2
y$dem[y$dem==1]<-2

pol.app<-c(x$pol_app1,y$pol_app2)
rep<-c(x$rep,y$rep)
dem<-c(x$dem,y$dem)

z<-as.data.frame(cbind(pol.app,rep,dem))



#chi^2 test for each distribution



z$rep1<-NA #rep indicator for first survey
z$rep1[z$rep==1]<-1
z$rep1[z$dem==1]<-0

z$rep2<-NA #rep indicator for second survey
z$rep2[z$rep==2]<-1
z$rep2[z$dem==2]<-0

library(MASS)

table(z$pol.app,z$rep)
chisq.test(table(z$pol.app,z$rep))

table(z$pol.app,z$dem)
chisq.test(table(z$pol.app,z$dem))

table(z$pol.app,z$rep1)
chisq.test(table(z$pol.app,z$rep1))

table(z$pol.app,z$rep2)
chisq.test(table(z$pol.app,z$rep2))

#recode pol.app so that chi-square approximation is correct

z$pol.app.trunc<-z$pol.app
z$pol.app.trunc[z$pol.app==4]<- 2
z$pol.app.trunc[z$pol.app==-4]<- -2

z$pol.app.trunc[z$pol.app==3]<- 2
z$pol.app.trunc[z$pol.app==-3]<- -2

table(z$pol.app.trunc,z$rep)
prop.table(table(z$pol.app.trunc,z$rep), 2)
chisq.test(table(z$pol.app.trunc,z$rep))

table(z$pol.app.trunc,z$dem)
chisq.test(table(z$pol.app.trunc,z$dem))

table(z$pol.app.trunc,z$rep1)
chisq.test(table(z$pol.app.trunc,z$rep1))

table(z$pol.app.trunc,z$rep2)
chisq.test(table(z$pol.app.trunc,z$rep2))
